/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ItemOrderGUI2.java
 *
 * Created on 23-06-2011, 10:24:49
 */
package GUILayer;
import ControlLayer.*;
import javax.swing.JOptionPane;
/**
 *
 * @author Jonas Kristensen
 */
public class ItemOrderGUI2 extends javax.swing.JFrame {
    private String barcode;
    private ItemController itemController;
    private ItemOrderGUI itemOrderGUI;
    /** Creates new form ItemOrderGUI2 */
    public ItemOrderGUI2(String itemBarcode, ItemOrderGUI itemOrder) {
        initComponents();
        itemOrderGUI = itemOrder;
        itemController = new ItemController();
        this.barcode = itemBarcode;
        displayItem();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        itemOrderGUI2Panel = new javax.swing.JPanel();
        barcodeLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        itemNbLabel = new javax.swing.JLabel();
        amountInStockLabel = new javax.swing.JLabel();
        minStockLabel = new javax.swing.JLabel();
        maxStockLabel = new javax.swing.JLabel();
        orderButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        amountYouWantToOrderLabel = new javax.swing.JLabel();
        amountYouWantToOrderTextField = new javax.swing.JTextField();
        amountPrOrderLabel = new javax.swing.JLabel();
        xAmountPrOrderLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        itemOrderGUI2Panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Order item"));

        barcodeLabel.setText("Barcode:");

        nameLabel.setText("Name:");

        itemNbLabel.setText("Item Nb:");

        amountInStockLabel.setText("Amount in stock:");

        minStockLabel.setText("Min. stock:");

        maxStockLabel.setText("Max. stock:");

        orderButton.setText("Order");
        orderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orderButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        amountYouWantToOrderLabel.setText("Amount you want to order:");

        amountYouWantToOrderTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                amountYouWantToOrderTextFieldActionPerformed(evt);
            }
        });

        amountPrOrderLabel.setText("Amount pr order:");

        xAmountPrOrderLabel.setText("x");

        javax.swing.GroupLayout itemOrderGUI2PanelLayout = new javax.swing.GroupLayout(itemOrderGUI2Panel);
        itemOrderGUI2Panel.setLayout(itemOrderGUI2PanelLayout);
        itemOrderGUI2PanelLayout.setHorizontalGroup(
            itemOrderGUI2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(itemOrderGUI2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(orderButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                        .addComponent(cancelButton)
                        .addGap(18, 18, 18))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addContainerGap(162, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(barcodeLabel)
                        .addContainerGap(150, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(itemNbLabel)
                        .addContainerGap(151, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(amountInStockLabel)
                        .addContainerGap(113, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(minStockLabel)
                        .addContainerGap(141, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(maxStockLabel)
                        .addContainerGap(137, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(amountPrOrderLabel)
                        .addContainerGap(110, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(amountYouWantToOrderLabel)
                        .addContainerGap(62, Short.MAX_VALUE))
                    .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                        .addComponent(amountYouWantToOrderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(xAmountPrOrderLabel)
                        .addContainerGap(135, Short.MAX_VALUE))))
        );
        itemOrderGUI2PanelLayout.setVerticalGroup(
            itemOrderGUI2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemOrderGUI2PanelLayout.createSequentialGroup()
                .addComponent(nameLabel)
                .addGap(5, 5, 5)
                .addComponent(barcodeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemNbLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amountInStockLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(minStockLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(maxStockLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amountPrOrderLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amountYouWantToOrderLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(itemOrderGUI2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(amountYouWantToOrderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(xAmountPrOrderLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(itemOrderGUI2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(orderButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(itemOrderGUI2Panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(itemOrderGUI2Panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void displayItem()
    {
        barcodeLabel.setText("Barcode: " + barcode);
        nameLabel.setText("Name: " + itemController.findItemByBarcode(barcode).getName());
        itemNbLabel.setText("Item Nb: " + itemController.findItemByBarcode(barcode).getItemNumber());
        amountInStockLabel.setText("Amount in stock: " + itemController.findItemByBarcode(barcode).getAmountInStock());
        minStockLabel.setText("Min. stock: " + itemController.findItemByBarcode(barcode).getMinStock());
        maxStockLabel.setText("Max. stock: " + itemController.findItemByBarcode(barcode).getMaxStock());
        amountPrOrderLabel.setText("Amount pr Order: " + itemController.findItemByBarcode(barcode).getAmountPrOrder());
        xAmountPrOrderLabel.setText("x " + itemController.findItemByBarcode(barcode).getAmountPrOrder());
    }
    
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void orderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orderButtonActionPerformed
        int amountToOrder = Integer.parseInt(amountYouWantToOrderTextField.getText());
        
        if(amountToOrder * itemController.findItemByBarcode(barcode).getAmountPrOrder() > itemController.findItemByBarcode(barcode).getMaxStock() - itemController.findItemByBarcode(barcode).getAmountInStock())
        {
            JOptionPane.showMessageDialog(this, "Your order is to big","Info",JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            itemController.findItemByBarcode(barcode).setOrderedBySupplier(true);
            itemOrderGUI.clearTextArea1();
            itemOrderGUI.clearTextArea2();
            itemOrderGUI.itemWhichHaveBeenOrdered();
            itemOrderGUI.itemWhichHaveToBeOrdered();
            this.dispose();
        }
    }//GEN-LAST:event_orderButtonActionPerformed

    private void amountYouWantToOrderTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_amountYouWantToOrderTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_amountYouWantToOrderTextFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel amountInStockLabel;
    private javax.swing.JLabel amountPrOrderLabel;
    private javax.swing.JLabel amountYouWantToOrderLabel;
    private javax.swing.JTextField amountYouWantToOrderTextField;
    private javax.swing.JLabel barcodeLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel itemNbLabel;
    private javax.swing.JPanel itemOrderGUI2Panel;
    private javax.swing.JLabel maxStockLabel;
    private javax.swing.JLabel minStockLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton orderButton;
    private javax.swing.JLabel xAmountPrOrderLabel;
    // End of variables declaration//GEN-END:variables
}
